package com.zonski.jbots.desktop.client.entity;

import java.awt.Image;

import com.zonski.jbots.client.IndexFactory;
import com.zonski.jbots.engine.Entity;
import com.zonski.jbots.engine.Directions;

/**
 * Obtains an image for a given entity
 */
public class ImageFactory
{
    private Image[][] images;
    protected IndexFactory indexFactory;

    public ImageFactory(Image[][] images, IndexFactory indexFactory)
    {
        this.images = images;
        this.indexFactory = indexFactory;
    }
        
    public Image getImage(Entity entity)
    {
        return this.getImage(entity, this.images, this.indexFactory);
    }

    public Image getImage(int layerId)
    {
        return images[0][0];
    }

    protected final Image getImage(Entity entity, Image[][] images, IndexFactory indexFactory)
    {
        try
        {
            int direction = entity.direction;
            int updates = entity.getEffectiveModeUpdates();
            int index = indexFactory.getIndex(updates);
            return images[direction][index];
        }catch(Exception ex){
            throw new RuntimeException("problem with renderer for "+entity.entityType.getName()+" in mode "+
                    entity.getMode(), ex);
        }
    }
}
